package com.csesteel.crm.control;

import com.csesteel.crm.domain.CrmMenu;
import com.csesteel.crm.domain.CrmPriviledge;
import com.csesteel.crm.domain.CrmRole;
import com.csesteel.crm.service.CrmMenuService;
import com.csesteel.crm.service.CrmPriviledgeService;
import com.csesteel.crm.service.CrmRoleService;
import com.csesteel.crm.util.WebUtil;
import com.csesteel.crm.vo.ResponseInfo;
import com.csesteel.crm.vo.ResponseMenuInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/crm")
public class RoleController extends BaseController{
    private Log logger = LogFactory.getLog(RoleController.class);
    @Autowired
    private CrmPriviledgeService crmPriviledgeService;

    @Autowired
    private CrmRoleService crmRoleService;



    /**
     * 根据分页获取所有的权限
     * @param request
     * @param response
     */
    @RequestMapping("/getRoleByPage")
    public void getRoleByPage(HttpServletRequest request,HttpServletResponse response) {
        int pageNumber =Integer.parseInt(request.getParameter("pageNumber"));
        int pageSize =Integer.parseInt(request.getParameter("pageSize"));
        String roleCode = request.getParameter("roleCode");
        String total = null;
        List<CrmRole> crmRoleList = crmRoleService.findAllCrmRoleByPage(pageNumber, pageSize, roleCode);
        total = String.valueOf(crmRoleService.findCount(roleCode));
        ResponseMenuInfo responseInfo = new ResponseMenuInfo();
        responseInfo.setList(crmRoleList);
        responseInfo.setTotalNumber(total);
        WebUtil.responseToJson(response,responseInfo);
    }

    /**
     * 修改角色
     * @param request
     * @param response
     */
    @RequestMapping("/updateRoleById")
    public void updateRoleById(@ModelAttribute("crmRole") CrmRole crmRole,HttpServletRequest request,HttpServletResponse response) {

        if (crmRole != null)
        {
            crmRoleService.updateCrmRole(crmRole);
        }
    }

    /**
     * 保存角色
     * @param request
     * @param response
     */
    @RequestMapping("/getCrmRoleByCode")
    public void getCrmRoleByCode(@ModelAttribute("crmRole") CrmRole crmRole,HttpServletRequest request,HttpServletResponse response) {
        ResponseInfo responseInfo = new ResponseInfo();
        RequestContext requestContext = new RequestContext(request);
        if (crmRole != null)
        {
            CrmRole crmRole1 = crmRoleService.getCrmRoleByCode(crmRole.getRoleCode());
            if(crmRole1 != null)
            {
                responseInfo.setResult("10005");
                responseInfo.setMessage(requestContext.getMessage("rolecode.repeat.again"));
            }else
            {
                responseInfo.setResult("10000");
                responseInfo.setMessage(requestContext.getMessage("rolecode.available"));
            }

            WebUtil.responseToJson(response, responseInfo);
        }
    }
    /**
     * 保存角色
     * @param request
     * @param response
     */
    @RequestMapping("/saveRoleById")
    public void saveRoleById(@ModelAttribute("crmRole") CrmRole crmRole,HttpServletRequest request,HttpServletResponse response) {
        ResponseInfo responseInfo = new ResponseInfo();
        RequestContext requestContext = new RequestContext(request);
        if (crmRole != null)
        {
            CrmRole crmRole1 = crmRoleService.getCrmRoleByCode(crmRole.getRoleCode());
            if(crmRole1 != null)
            {
                responseInfo.setResult("10005");
                responseInfo.setMessage(requestContext.getMessage("rolecode.repeat.again"));
            }else
            {
                crmRoleService.saveCrmRole(crmRole);
                responseInfo.setResult("10000");
                responseInfo.setMessage(requestContext.getMessage("role.success"));
            }

            WebUtil.responseToJson(response, responseInfo);
        }
    }

    /**
     * 删除角色
     * @param request
     * @param response
     */
    @RequestMapping("/deleteRoleById")
    public void deleteRoleById(HttpServletRequest request,HttpServletResponse response) {

        String roleId =request.getParameter("roleId");
        crmRoleService.deleteCrmRole(Integer.parseInt(roleId));
    }

    /**
     * 获取角色对应的权限
     * @param request
     * @param response
     */
    @RequestMapping("/getPriviledgeByRole")
    public void getPriviledgeByRole(HttpServletRequest request,HttpServletResponse response) {
        String roleId = request.getParameter("roleId");
        if(roleId != null && !"".equals(roleId))
        {
            int iroleId =Integer.parseInt(request.getParameter("roleId"));
            CrmRole crmRole = crmRoleService.getCrmRole(iroleId);
            List<CrmPriviledge> crmPriviledgeList = crmPriviledgeService.findAllCrmPriviledge();
            ResponseMenuInfo responseInfo = new ResponseMenuInfo();
            responseInfo.setList(crmPriviledgeList);
            responseInfo.setObject(crmRole);
            WebUtil.responseToJson(response,responseInfo);
        }

    }

    /**
     * 保存角色权限对应的资源
     * @param request
     * @param response
     */
    @RequestMapping("/savePriviledgeByRole")
    public void savePriviledgeByRole(HttpServletRequest request,HttpServletResponse response) {
        RequestContext requestContext = new RequestContext(request);
        String roleId = request.getParameter("roleId");
        String[] priviledgeIds = request.getParameterValues("priviledgeId[]");
        if(roleId != null && !"".equals(roleId))
        {
            int iroleId =Integer.parseInt(request.getParameter("roleId"));
            crmRoleService.deletePriviledgeByRole(iroleId); //删除关联的权限
            if(priviledgeIds !=null && priviledgeIds.length>0)
            {
                for(String priviledgeId :priviledgeIds)
                {
                    int ipriviledgeId = Integer.parseInt(priviledgeId);
                    crmRoleService.savePriviledgeByRole(ipriviledgeId, iroleId);
                }
                ResponseMenuInfo responseInfo = new ResponseMenuInfo();
                responseInfo.setResult("0");
                responseInfo.setMessage(requestContext.getMessage("privilege.menu.success"));
                WebUtil.responseToJson(response,responseInfo);
            }

        }

    }
}